class Solution {
public:
    int maxDepth(TreeNode* root) {
        if (root == NULL) {
            return 0;
        }
        return std::max(maxDepth(root->left), maxDepth(root->right)) + 1;
    }
    bool isBalanced(TreeNode* root) {
        if (root) {
            return isBalanced(root->left) && isBalanced(root->right)
                   && abs(maxDepth(root->left) - maxDepth(root->right)) <= 1;
        }
        return true;
    }
};
